<<<<<<< HEAD
<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/1/5
 * Time: 14:03
 */
namespace common\services\WXpay;

use Yii;
use common\models\kaoguobao\Order;
use common\models\kaoguobao\WechatNotify;
use common\services\WXpay\Wechat\JsApiPay;
use common\services\WXpay\lib\WxPayApi;
use common\services\WXpay\lib\WxPayUnifiedOrder;


class WeiXinPayService{

    public $appId;
    public $appSecret;
    public $grantType;
    public $mchId;
    public $payKey;

    public function __construct()
    {
        $this->config       = Yii::$app->params['wx'];      // 获取微信相关配置
        $this->appId        = $this->config['app_id'];       // 公众号ID
        $this->appSecret    = $this->config['secret'];   // 公众号key
        $this->grantType    = $this->config['token'];   // 获取token参数
        $this->mchId        = $this->config['mchId'];       // 商户id
        $this->payKey       = $this->config['payKey'];      // 支付key
        $this->notifyUrl    = $this->config['notifyUrl'];   // 回调域名
    }

    /**
     * 微信支付统一下单
     * @param string $orderBody
     * @param string $orderNum
     * @param int $amount
     * @param string $openId
     * @return array
     */
    public function getPay($orderBody = '', $orderNum = '', $amount = 0, $openId = '')
    {
        $input = new WxPayUnifiedOrder();
        $input->SetBody($orderBody);                                              // 设置商品或支付单简要描述
        $input->SetAttach($orderBody);
        $input->SetOut_trade_no($orderNum);                                       // 商户订单号
        $input->SetTotal_fee($amount);                                            // 支付金额
        $input->SetTime_start(date("YmdHis"));                              // 订单生效时间
        $input->SetTime_expire(date("YmdHis", time() + 3600));    // 订单过期时间
        $input->SetGoods_tag($orderBody);
        $input->SetNotify_url($this->notifyUrl);                                  // 授权地址
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $tools = new JsApiPay();
        $jsApiParameters = $tools->GetJsApiParameters($order);
        $editAddress     = $tools->GetEditAddressParameters();
        $result = [
            'jsApiParameters' => $jsApiParameters,
            'editAddress' => $editAddress
        ];
//        $editAddress = $tools->GetEditAddressParameters();
        return $result;
    }

    /**
     * 微信支付回调之后的业务处理
     * 订单修改
     * 微信回调信息的存储
     * @param array $notify
     */
    public function wechatNotify($notify = array())
    {
        if(empty($notify) || !is_array($notify)){
            return;
        }
        if(empty($notify['return_code']) || $notify['return_code'] != "SUCCESS"){
            return;
        }
        // 对订单进行修改
        $orderData = Order::find()->where(['order_num' => $notify['out_trade_no']??''])->limit(1)->one();
        if(empty($orderData)){
            return;
        }
        if($orderData->order_status == Order::ORDER_STATUS_STAY_PAY){
            $orderData->order_status = Order::ORDER_STATUS_PAY_OK;
            $orderData->start_time   = time();
            $orderData->save();
        }
        // 进行微信回调数据的存储
        $wechatData = WechatNotify::find()->where(['order_num' => $notify['out_trade_no']??''])->limit(1)->one();
        if(empty($wechatData)){
            $wechatData = new WechatNotify();
        }
        $wechatData->order_id           = $orderData->id;
        $wechatData->order_num          = $orderData->order_num;
        $wechatData->wechat_notify_json = json_encode($notify);
        $wechatData->save();
    }
=======
<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2018/1/5
 * Time: 14:03
 */
namespace common\services\WXpay;

use Yii;

use common\services\WXpay\Wechat\JsApiPay;
use common\services\WXpay\lib\WxPayApi;
use common\services\WXpay\lib\WxPayUnifiedOrder;


class WeiXinPayService{

    public $appId;
    public $appSecret;
    public $grantType;
    public $mchId;
    public $payKey;

    public function __construct()
    {
        $this->config       = Yii::$app->params['wx'];      // 获取微信相关配置
        $this->appId        = $this->config['appId'];       // 公众号ID
        $this->appSecret    = $this->config['appSecret'];   // 公众号key
        $this->grantType    = $this->config['grantType'];   // 获取token参数
        $this->mchId        = $this->config['mchId'];       // 商户id
        $this->payKey       = $this->config['payKey'];      // 支付key
        $this->notifyUrl    = $this->config['notifyUrl'];   // 回调域名
    }

    public function getPay($orderBody = '', $orderNum = '', $amount = 0, $openId = '')
    {
        $input = new WxPayUnifiedOrder();
        $input->SetBody($orderBody);                                              // 设置商品或支付单简要描述
        $input->SetAttach($orderBody);
        $input->SetOut_trade_no($orderNum);                                       // 商户订单号
        $input->SetTotal_fee($amount);                                            // 支付金额
        $input->SetTime_start(date("YmdHis"));                              // 订单生效时间
        $input->SetTime_expire(date("YmdHis", time() + 3600));    // 订单过期时间
        $input->SetGoods_tag($orderBody);
        $input->SetNotify_url($this->notifyUrl);                                  // 授权地址
        $input->SetTrade_type("JSAPI");
        $input->SetOpenid($openId);
        $order = WxPayApi::unifiedOrder($input);
        $tools = new JsApiPay();
        $jsApiParameters = $tools->GetJsApiParameters($order);
//        $editAddress = $tools->GetEditAddressParameters();
        return $jsApiParameters;
    }
>>>>>>> a6e81d742fd38b6b9a8d832bc802be5818b4a6fa
}